/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-25
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Coupon;
import com.rzico.basics.entity.CouponCode;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.CouponCodeService;
import com.rzico.basics.service.CouponService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兑换券发券接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-25
 */
@Api(description = "兑换券发券接口")
@RestController
@RequestMapping("/game/coupon")
public class GameCouponController extends BaseController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponCodeService couponCodeService;

    @Autowired
    private SysSequenceService sysSequenceService;

    /**
     * 游戏发券接口
     *
     * @param couponId
     * @return
     */
    @ApiOperation("游戏发券接口")
    @PostMapping("/create")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户号(珍农:10200)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "注册手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponId", value = "券ID", dataType = "Integer", paramType = "query")
    })
    public CommResult<CouponCode> create(String mchId,String mobile,Long couponId) {

        if (mchId==null) {
            return CommResult.error("无效商户号");
        }

        if (mobile==null) {
            return CommResult.error("无效手机号");
        }

        Member member = memberService.findByMchMobile(mchId,mobile);
        if (member==null) {
            return CommResult.error("会员没有注册");
        }

        Coupon coupon = couponService.findById(couponId);
        if (coupon==null) {
            return CommResult.error("无效优惠券");
        }

        if (coupon.getDeleted()) {
            return CommResult.error("已结束了");
        }

        if (coupon.getStock()<=0) {
            return CommResult.error("已领完了");
        }

        if (coupon.getMaxgiven()==null) {
            coupon.setMaxgiven(0L);
        }

        if (coupon.getMaxgiven()>0) {
            Map<String,Object> params = new HashMap<>();
            params.put("couponId",coupon.getId());
            params.put("memberId",member.getId());
            Integer w = couponCodeService.selectRowCount(params);
            if (w>=coupon.getMaxgiven()) {
                return CommResult.error("不能再领了");
            }
        }

        String sn = sysSequenceService.generate("couponCode");

        couponCodeService.create(coupon,member,sn,1);

        return CommResult.success("领取成功");

    }


}
